package com.gscitysfy.cus.drain.utils;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.converts.PostgreSqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.PostgreSqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.keywords.PostgreSqlKeyWordsHandler;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author Administrator
 */
public class MybatisPlusGenerator {
    public static void main(String[] args) {
        String author = System.getenv("zhaoruirui");
        doGenerator(author
                , "pipe"
                , Arrays.asList("drain_pipe_network_flaw")
        );
    }
    /**
     * @param author
     * @param moduleName           生成的业务模块目录名称，默认设定com.zeone.lifeline.modules下一级目录名
     * @param tables               需要自动生成的表名
     * @param disableTemplateTypes 不需要生成的类模板
     */
    public static void doGenerator(String author, String moduleName, List<String> tables, TemplateType... disableTemplateTypes) {
        Assert.notNull(moduleName, "moduleName 不能为null");
        Assert.hasLength(moduleName, "moduleName 不能为空");
        Assert.notEmpty(tables, "tables 不能为空");
        Assert.noNullElements(tables, "tables 不能为null");

        String projectDir = System.getProperty("user.dir");
        String javaDir = projectDir + "\\src\\main\\java\\";
        String mapperXmlDir = projectDir + "\\src\\main\\resources\\mapping\\" + moduleName;

        DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig.Builder("jdbc:postgresql://10.100.120.6:25432/gscsfy_l3_draindb_v1", "postgres", "gscitysfy")
                .dbQuery(new PostgreSqlQuery())
                .schema("drain_dd")
                .typeConvert(new PostgreSqlTypeConvert())
                .keyWordsHandler(new PostgreSqlKeyWordsHandler());

        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            // .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(javaDir) // 指定输出目录
                            .dateType(DateType.TIME_PACK)
                            .disableOpenDir()
                            .commentDate("yyyy-MM-dd HH:mm:ss");
                })
                .packageConfig(builder -> {
                    // 设置父包名
                    builder.parent("com.gscitysfy.cus.drain.modules")
                            .moduleName(moduleName) // 设置父包模块名
                            .entity("entity")
                            .service("service")
                            .serviceImpl("service.impl")
                            .mapper("mapper")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(
                                    // 设置mapperXml生成路径
                                    OutputFile.mapperXml, mapperXmlDir
                            ));
                })
                .strategyConfig((scanner, builder) -> builder.addInclude(tables)
                        .mapperBuilder().enableBaseColumnList()
                        .enableBaseResultMap()
                        .entityBuilder().enableLombok().build())

                .templateConfig(builder -> {
                    builder.disable(
                            disableTemplateTypes
                    ).build();
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
